package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.mysql.MessageSettingsDao;
import com.juyu.miliao.api.dao.mysql.UserCountDao;
import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.dao.mysql.UserMessageSettingsDao;
import com.juyu.miliao.api.domain.MessageSettings;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.domain.UserCount;
import com.juyu.miliao.api.domain.UserMessageSettings;
import com.juyu.miliao.api.domain.dto.IntegralLevelAndTitleDTO;
import com.juyu.miliao.api.domain.vo.MessageVo;
import com.juyu.miliao.api.exception.WrongErrorMoney;
import com.juyu.miliao.api.exception.WrongNoMessageIdException;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import com.juyu.miliao.common.exception.IllegalParamException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Auther: 卜加超
 * @Date: 2019/1/16 18:19
 * @Description:
 */
@Service
public class MessageSettingsService {
    private static final Logger logger = LoggerFactory.getLogger(MessageSettingsService.class);
    @Resource
    private MessageSettingsDao messageSettingsDao;
    @Resource
    private UserMessageSettingsDao userMessageSettingsDao;
    @Resource
    private UserInfoDao userInfoDao;
    @Resource
    private IntegralLevelInfoService integralLevelInfoService;

    public List<MessageSettings> getMessageSettingsList(long userId, int type) {
        UserMessageSettings ums = userMessageSettingsDao.selectUserMessageSettingsListByUserId(userId);
        User user = userInfoDao.getUserInfoById(userId);
        List<MessageSettings> list = new ArrayList<>();
        if (user != null && ums != null && user.getIsCertificationHost() == 1) {
            List<MessageSettings> messageSettingList = messageSettingsDao.selectMessageSettingsList(type);
            for (MessageSettings m : messageSettingList) {
                //男用户通话设置
                if (user.getSex() == 1) {
                    if(m.getGoldPrice()!=0){
                        //如果当前接听的成功人数大于等于设置的成功人数 可选  如果小于当前成功的接通人数 不可选
                        if (m.getMessageType() == 1) {
                            if (m.getSucessPeople() <= 10) {
                                m.setIsChoose(1);
                            }else if (user.getCallSuccessSumVoice() >= m.getSucessPeople()) {
                                m.setIsChoose(1);
                            } else {
                                m.setIsChoose(2);
                            }
                            m.setShowMessageId(ums.getMessageVoiceId());
                            list.add(m);
                        } else {
                            if (m.getSucessPeople() <= 10) {
                                m.setIsChoose(1);
                            } else if (user.getCallSuccessSumVideo() >= m.getSucessPeople()) {
                                m.setIsChoose(1);
                            } else {
                                m.setIsChoose(2);
                            }
                            m.setShowMessageId(ums.getMessageVideoId());
                            list.add(m);
                        }
                    }
                } else {
                    //女用户的通话设置
                    if (m.getMessageType() == 1) {
                         if (user.getCallSuccessSumVoice() >= m.getSucessPeople()) {
                            m.setIsChoose(1);
                        } else {
                            m.setIsChoose(2);
                        }
                        m.setShowMessageId(ums.getMessageVoiceId());
                        list.add(m);
                    } else {
                        if (user.getCallSuccessSumVideo() >= m.getSucessPeople()) {
                            m.setIsChoose(1);
                        } else {
                            m.setIsChoose(2);
                        }
                        m.setShowMessageId(ums.getMessageVideoId());
                        list.add(m);
                    }
                }
            }

        }
        return list;
    }



    public List<UserMessageSettings> getUserMessageSettingsList(long userId) {
        User user = userInfoDao.getUserInfoById(userId);
        UserMessageSettings userMessageSettings1 = userMessageSettingsDao.selectUserMessageSettingsListByUserId(userId);
        List<UserMessageSettings> list = new ArrayList<>();

        if(user!=null && user.getIsCertificationHost()==1 && userMessageSettings1!=null){

             MessageSettings messageSettings = messageSettingsDao.
                    selectMessageSettingsById(userMessageSettings1.getMessageVoiceId());
             MessageSettings messageSettings1 = messageSettingsDao.
                     selectMessageSettingsById(userMessageSettings1.getMessageVideoId());
            if(messageSettings.getMessageType()==1) {
                //获取语音部分的列表
                UserMessageSettings userMessageSettings = new UserMessageSettings();
                userMessageSettings.setUserId((int) user.getUserId());
                userMessageSettings.setMessageType(messageSettings.getMessageType());
                //设置当前语音接通人数
                userMessageSettings.setPresentSucessPeople(user.getCallSuccessSumVoice() + "");
                //获取当前金币价格
                userMessageSettings.setPresentGoldPrice(messageSettings.getGoldPrice());
                //修改用户视频价格
                userInfoDao.updateUserInfoByVoiceGoldPrice(String.valueOf(messageSettings.getGoldPrice()), user.getUserId());
                //获取语音开关
                userMessageSettings.setMessageSwitch(user.getIsCall());
                list.add(userMessageSettings);
            }
            if(messageSettings1.getMessageType()==2){

                UserMessageSettings userMessageSettings2 = new UserMessageSettings();
                userMessageSettings2.setUserId((int)user.getUserId());
                userMessageSettings2.setMessageType(messageSettings1.getMessageType());
                //设置当前视频接通人数
                userMessageSettings2.setPresentSucessPeople(String.valueOf(user.getCallSuccessSumVideo()));
                //获取当前金币价格
                userMessageSettings2.setPresentGoldPrice(messageSettings1.getGoldPrice());
                //修改用户视频价格
                userInfoDao.updateUserInfoByVideoGoldPrice(String.valueOf(messageSettings1.getGoldPrice()), user.getUserId());
                //获取视频开关
                userMessageSettings2.setMessageSwitch(user.getIsVideo());
                list.add(userMessageSettings2);
                }
            }
        return list;
    }

    public void updateUserMessageId(long userId, int type, int showMessageId) {
        User user = userInfoDao.getUserInfoById(userId);
        if(user != null && user.getIsCertificationHost()==1){

            MessageSettings msg = messageSettingsDao.selectMessageSettingsById(showMessageId);
            //修改用户语音-视频价格
            if(type==1){
                userMessageSettingsDao.updateUserMessageByVoiceId(userId,showMessageId);
                userInfoDao.updateUserInfoByVoiceGoldPrice(msg.getGoldPrice()+"",user.getUserId());
            }else{
                userMessageSettingsDao.updateUserMessageByVideoId(userId,showMessageId);
                userInfoDao.updateUserInfoByVideoGoldPrice(msg.getGoldPrice()+"",user.getUserId());
            }
        }else{
            throw new ConsumeRuntimeException("参数错误，更新失败");
        }
    }

    /**
     * 主播获取视频通话设置
     * @param id
     * @return
     */
    public List<MessageVo> getHostUserMessageSettingList(long id) {
        JSONObject jsonObject = new JSONObject();
        User user = userInfoDao.getUserInfoById(id);
        if (user == null)
            throw new IllegalParamException("用户不存在");
        IntegralLevelAndTitleDTO userIntegralLevel = integralLevelInfoService.getUserIntegralLevel(user.getIntegralSum());
         List<IntegralLevelAndTitleDTO> list = integralLevelInfoService.getList();
        List<IntegralLevelAndTitleDTO> levelList = new ArrayList<>();
        Set<Integer> set = new LinkedHashSet<>();
        for (IntegralLevelAndTitleDTO integralLevelAndTitleDTO: list ) {
            if(!set.contains(integralLevelAndTitleDTO.getGoldCoinsPerMinute())){
                set.add(integralLevelAndTitleDTO.getGoldCoinsPerMinute());
                levelList.add(integralLevelAndTitleDTO);
            }
        }
        List<MessageVo> messageVoList = new ArrayList<>();
        for (IntegralLevelAndTitleDTO integralDTO:levelList) {
            MessageVo messageVo = new MessageVo();
//            if(userIntegralLevel.getIntegralGrade()==1){
//                messageVo.setCoinsPerMinute(100);
//                messageVo.setIsChoose(1);
//                messageVo.setCurrentOptions(1);
//                messageVo.setShowCoinsPerMinute("100寻币/分钟");
//            }else {

                messageVo.setCoinsPerMinute(integralDTO.getGoldCoinsPerMinute());
                messageVo.setShowCoinsPerMinute(integralDTO.getGoldCoinsPerMinute()+"寻币/分钟");
               if(integralDTO.getIntegralGrade()<= userIntegralLevel.getIntegralGrade()){
                    if(userIntegralLevel.getIntegralGrade()==1){
                        messageVo.setIsChoose(1);
                    }else {
                        messageVo.setIsChoose(1);
                    }

                }
                if(messageVo.getIsChoose()==1){
                    messageVo.setShowCoinsPerMinute(integralDTO.getGoldCoinsPerMinute()+"寻币/分钟");
                }else {
                    messageVo.setShowCoinsPerMinute(integralDTO.getGoldCoinsPerMinute()+"寻币/分钟"+"("+integralDTO.getIntegralGrade()+"级开放）");
                }
                if(user.getVideoPrice().equals(integralDTO.getGoldCoinsPerMinute()+"")){
                    messageVo.setCurrentOptions(1);
                }

//            }
            messageVoList.add(messageVo);
        }
        return messageVoList;
    }

    public void userChooseMessageSetting(long id, int coinsPerMinute) {
       User user = userInfoDao.getUserInfoById(id);
        if (user == null)
            throw new IllegalParamException("用户不存在");
       IntegralLevelAndTitleDTO userIntegralLevel = integralLevelInfoService.getUserIntegralLevel(user.getIntegralSum());
        if(Integer.parseInt(user.getVideoPrice())>userIntegralLevel.getGoldCoinsPerMinute()){
           userInfoDao.updateVideoPrice(id,userIntegralLevel.getGoldCoinsPerMinute());
       }else {
            if(coinsPerMinute!=0){
                userInfoDao.updateVideoPrice(id,coinsPerMinute);
            }
        }

    }
}
